package nsg2;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class EditTcl {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        
        final JTextArea text = new JTextArea();
        final String[] datas = new String[]{"123123", "234123", "345123", "456123", "567123", "678123","abc","bcd"};
        
        JMenuBar menuBar = new JMenuBar();
        JMenu file = new JMenu("File");
        JMenuItem save = new JMenuItem("save");
        JMenuItem saveAs = new JMenuItem("save as");
        
        menuBar.add(file);
        file.add(save);
        file.add(saveAs);
        
        save.addActionListener( new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					java.awt.Desktop.getDesktop().edit(new java.io.File("C:\\Program Files"));
//					java.awt.Desktop.getDesktop().edit(new File("C:\\a.txt"));
//					java.awt.Desktop.getDesktop().open(new java.io.File("C:\\Program Files"));
//					java.awt.Desktop.getDesktop().open(file)
					
					
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
        
        text.addKeyListener(new KeyAdapter() {

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				if( text.getText() == null || text.getText().length() == 0){
					return;
				}
				String []letter = text.getText().split(" ");
				if( e.getKeyCode() == 32 ){
					letter = new String[]{" "};
				}
			
				for( int j = 0 ; j < datas.length ; j++){
					if(datas[j].toLowerCase().startsWith(letter[letter.length-1])){
						
						text.setText(text.getText() + datas[j].substring(1));
						break;
					}
				}
				
				
			}
        	
		});
        
        frame.getContentPane().add(new JScrollPane(text), BorderLayout.CENTER);
        frame.getContentPane().add(menuBar,BorderLayout.NORTH);
        frame.setDefaultCloseOperation(3);
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
